﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DalatCooking.Core;
using DalatCooking.Core.Helper;
public partial class admin_Menu_Group : System.Web.UI.Page
{


    int total = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGrid();

            //  Categories.LoadParentDropDown(ddlNewParent, 0);
        }

        grid.RowDeleting += new GridViewDeleteEventHandler(grid_RowDeleting);
        grid.RowUpdating += new GridViewUpdateEventHandler(grid_RowUpdating);
        grid.RowEditing += new GridViewEditEventHandler(grid_RowEditing);
        grid.RowCancelingEdit += delegate { Response.Redirect(Request.RawUrl); };
        grid.RowDataBound += new GridViewRowEventHandler(grid_RowDataBound);


    }
    protected override void OnInit(EventArgs e)
    {
        if (!IsPostBack)
        {
            Person p = Session.GetPersonLogged();
            if (!p.IsInRole("SuperUser"))
                Response.Redirect("/admin/accessdeny.aspx");
        }

        //ddlnewparent.datasource = categories.all();
        //ddlnewparent.datatextfield = "categoryname";
        //ddlnewparent.datavaluefield = "categoryid";
        //ddlnewparent.databind();


        base.OnInit(e);
    }

    void BindGrid()
    {

        grid.DataSource = MenuGroupManager.FindAll();
        grid.DataBind();
    }

    public int PublishedID
    {
        get
        {
            if (Request.QueryString["PublishedID"] != null)
                return ConvertObject.ToInt(Server.HtmlEncode(Request.QueryString["PublishedID"]));
            else return -1;
        }
    }

    public int Count()
    {

        return total;
    }



    protected string GetParentTitle(object obj)
    {
        MenuGroup temp = MenuGroupManager.Single(int.Parse(obj.ToString()));
        if (temp == null)
            return "";
        else

            return temp.Name;
    }


    public string CountMenu(object obj)
    {
        return DalatCooking.Core.MenuManager.FindByGroupId(ConvertObject.ToInt(obj)).Count.ToString();

    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            MenuGroup g = new MenuGroup { Name = txtNewPageTitle.Text, UniqueName = StringHelper.RemoveDiacriticsURL(txtNewPageTitle.Text), GroupID = -1, ApplicationId = 1 };
            if(MenuGroupManager.Insert(g))
                Response.Redirect(Request.RawUrl);
        }
    }
    public void LoadFiles(DropDownList ddl)
    {
        //string imageFolder = Settings.Setting("file.imagefolder");
        //List<string> list = new List<string>();
        //DirectoryInfo dirInfo = new DirectoryInfo(Server.MapPath(imageFolder));

        //FileInfo[] files = dirInfo.GetFiles();
        //foreach (FileInfo file in files)
        //{
        //    if (Path.GetExtension(file.Name).Equals(".jpg"))
        //    {
        //        ddl.Items.Add(new ListItem(file.Name, imageFolder + file.Name));
        //    }
        //}
    }
    void grid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        /*
        if (e.Row.RowState == DataControlRowState.Edit ||
                e.Row.RowState == (DataControlRowState.Alternate | DataControlRowState.Edit))
        {
            Categories self = (Categories)e.Row.DataItem;
            DropDownList ddlParent = (DropDownList)e.Row.FindControl("ddlParent");
            DropDownList ddlImage = (DropDownList)e.Row.FindControl("ddlImage");
            CheckBox chkIsPublishedEdit = (CheckBox)e.Row.FindControl("chkIsPublishedEdit");
            HiddenField txtImage = (HiddenField)e.Row.FindControl("txtImage");
            chkIsPublishedEdit.Checked = self.IsPublished;
            Categories.LoadParentDropDown(ddlParent, 0);
            LoadFiles(ddlImage);
            Categories temp = (Categories)e.Row.DataItem;
            foreach (ListItem item in ddlImage.Items)
            {
                if (item.Value == txtImage.Value)
                {
                    item.Selected = true;
                    break;
                }
            }
            if (temp.ParentID != 0)
            {
                foreach (ListItem item in ddlParent.Items)
                {
                    if (item.Value == temp.ParentID.ToString())
                    {
                        item.Selected = true;
                        break;
                    }
                }
            }
        }
         * */
    }
    void grid_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grid.EditIndex = e.NewEditIndex;
        BindGrid();
    }
    void grid_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int id = (int)grid.DataKeys[e.RowIndex].Value;
        TextBox txtTitle = (TextBox)grid.Rows[e.RowIndex].FindControl("txtTitle");

        TextBox txtSortOrder = (TextBox)grid.Rows[e.RowIndex].FindControl("txtSortOrder");
        DropDownList ddlParent = (DropDownList)grid.Rows[e.RowIndex].FindControl("ddlParent");
        CheckBox chkIsPublishedEdit = (CheckBox)grid.Rows[e.RowIndex].FindControl("chkIsPublishedEdit");
        DropDownList ddlImage = (DropDownList)grid.Rows[e.RowIndex].FindControl("ddlImage");

        MenuGroup mg = MenuGroupManager.Single(id);
        mg.Name = txtTitle.Text;
        if (MenuGroupManager.Update(mg))
            Response.Redirect("Group.aspx");
    }
    void grid_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

        int id = (int)grid.DataKeys[e.RowIndex].Value;
        if (MenuGroupManager.Delete(id))
            Response.Redirect("Group.aspx");
    }
}
